/*
 * Config.java
 * 
 * This file is part of Netuno.
 * 
 * CajuScript is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3, or (at your option) 
 * any later version.
 * 
 * CajuScript is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with CajuScript.  If not, see <http://www.gnu.org/licenses/>.
*/

package org.netuno;

import org.netuno.psamata.Values;
import org.netuno.psamata.script.BasicFuncs;
import org.netuno.psamata.mail.SendMail;
import org.netuno.psamata.script.ScriptRunner;
import javax.script.ScriptException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.ServerSocket;

/**
 * Configuration.
 * @author eduveks
 */
public final class Config {
    /**
     * Configuration.
     */
    private Config() { }
    /**
     * Admin E-mail From.
     */
    private static String adminEmailFrom = "";
    /**
     * Admin E-mail To.
     */
    private static String adminEmailTo = "";
    /**
     * Admin E-mail Server.
     */
    private static String adminEmailServer = "";
    /**
     * Port.
     */
    private static int port = 80;
    /**
     * System Directory.
     */
    private static String systemDir = ".";
    /**
     * Jobs Interval.
     */
    private static long jobsInterval = 300000;
    /**
     * Time Out.
     */
    private static int timeOut = 900000;
    /**
     * Is Starting.
     */
    private static boolean starting = true;
    /**
     * Threads.
     */
    private static int threads = 50;
    /**
     * Memory Alert.
     */
    private static int memoryAlert = 25;
    /**
     * Client Socket Time Out
     */
    private static int clientSocketTimeOut = 1000;
    /**
     * Config Global.
     */
    private static Values config = new Values();
    /**
     * Socket Server.
     */
    private static ServerSocket serverSocket = null;
    /**
     * Get Admin E-mail From.
     * @return Admin E-mail From
     */
    public static String getAdminEmailFrom() {
        return adminEmailFrom;
    }
    /**
     * Set Admin E-mail From, for send alerts.
     * @param param Admin E-mail From
     */
    public static void setAdminEmailFrom(final String param) {
        Config.adminEmailFrom = param;
    }
    /**
     * Get Admin E-mail To.
     * @return Admin E-mail To
     */
    public static String getAdminEmailTo() {
        return adminEmailTo;
    }
    /**
     * Set Admin E-mail To, for send alerts.
     * @param param Admin E-mail To
     */
    public static void setAdminEmailTo(final String param) {
        Config.adminEmailTo = param;
    }
    /**
     * Get Admin E-mail Server.
     * @return Admin E-mail Server
     */
    public static String getAdminEmailServer() {
        return adminEmailServer;
    }
    /**
     * Set Admin E-mail Server, for send alerts.
     * @param param Admin E-mail Server
     */
    public static void setAdminEmailServer(final String param) {
        Config.adminEmailServer = param;
    }
    /**
     * Get Port of the Server Socket.
     * @return port
     */
    public static int getPort() {
        return port;
    }
    /**
     * Set Port of the Server Socket.
     * @param param Port
     */
    public static void setPort(final int param) {
        Config.port = param;
    }
    /**
     * Get path of the Application.
     * @return system dir
     */
    public static String getSystemDir() {
        return systemDir;
    }
    /**
     * Set path of the Application.
     * @param param system dir
     */
    public static void setSystemDir(final String param) {
        Config.systemDir = param;
    }
    /**
     * Get interval time to run jobs.
     * @return jobs interval
     */
    public static long getJobsInterval() {
        return jobsInterval;
    }
    /**
     * Set interval time to run jobs.
     * @param param jobs interval
     */
    public static void setJobsInterval(final long param) {
        Config.jobsInterval = param;
    }
    /**
     * Get time out of communication between server and client,
     * default is 900000 (equals the 15 minutes).
     * @return time out
     */
    public static int getTimeOut() {
        return timeOut;
    }
    /**
     * Set time out of communication between server and client,
     * default is 900000 (equals the 15 minutes).
     * @param param Time out
     */
    public static void setTimeOut(final int param) {
        Config.timeOut = param;
    }
    /**
     * Is the first service.
     * @return starting
     */
    public static boolean isStarting() {
        return starting;
    }
    /**
     * Set if is or not the first service, to rebuild.
     * @param param starting
     */
    public static void setStarting(final boolean param) {
        Config.starting = param;
    }
    /**
     * Get Threads limit in execution.
     * @return threads
     */
    public static int getThreads() {
        return threads;
    }
    /**
     * Set Threads limit in execution.
     * @param param threads
     */
    public static void setThreads(final int param) {
        Config.threads = param;
    }
    /**
     * Get Memory Alert.
     * @return memoryAlert
     */
    public static int getMemoryAlert() {
        return memoryAlert;
    }
    /**
     * Set Memory Alert.
     * @param param memoryAlert
     */
    public static void setMemoryAlert(final int param) {
        Config.memoryAlert = param;
    }
    /**
     * Get Client Socket Time Out.
     * @return timeOut
     */
    public static int getClientSocketTimeOut() {
        return clientSocketTimeOut;
    }
    /**
     * Set Client Socket Time Out.
     * @param timeOut Time out
     */
    public static void setClientSocketTimeOut(final int timeOut) {
        Config.clientSocketTimeOut = timeOut;
    }
    /**
     * Get Config.
     * @return Config
     */
    public static Values getConfig() {
        return config;
    }
    /**
     * Set Config.
     * @param param Config
     */
    public static void setConfig(final Values param) {
        Config.config = param;
    }
    /**
     * Get Server Socket.
     * @return serverSocket
     */
    public static ServerSocket getServerSocket() {
        return serverSocket;
    }
    /**
     * Set Server Socket.
     * @param param serverSocket
     */
    public static void setServerSocket(final ServerSocket param) {
        Config.serverSocket = param;
    }
    /**
     * Load Config.
     */
    public static void load() {
        systemDir = BasicFuncs.replace(
            System.getProperty("user.dir"), "\\", "/"
        );
    }
    /**
     * Load file configuration.
     * @throws javax.script.ScriptException Script Exception
     * @throws java.io.FileNotFoundException File not found
     * @throws java.io.IOException IO Exception
     */
    public static void loadFile() throws ScriptException, FileNotFoundException, IOException {
        String path = ScriptRunner.searchFile("netuno_config");
        if (path != null) {
            new ScriptRunner().runFile(path);
        }
    }
    /**
     * Send Admin E-Mail.
     * @param subject Subject
     * @param message Message
     */
    public static void sendAdminMail(
        final String subject,
        final String message
        ) {
        SendMail sendMail = new SendMail();
        sendMail.setFrom(Config.getAdminEmailFrom());
        sendMail.setTo(Config.getAdminEmailTo());
        sendMail.setSubject(subject);
        sendMail.setText(message);
        sendMail.setServer(Config.getAdminEmailServer());
        sendMail.sendMail();
    }
    /**
     * Is send admin e-mail actived.
     * @return Is actived
     */
    public static boolean isSendAdminMailActived() {
        if (!Config.getAdminEmailFrom().equals("")
        && !Config.getAdminEmailTo().equals("")
        && !Config.getAdminEmailServer().equals("")
        ) {
            return true;
        } else {
            return false;
        }
    }
}
